package org.example.business;

import com.alibaba.fastjson.JSONArray;
import com.amazonaws.util.StringUtils;
import com.google.actions.api.smarthome.*;
import org.example.utils.RequestParseParamsTool;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 *  说明: S_DingDong_Gateway
 * @author author
 * @date 2018年9月10日
 */
@RestController
public class S_Google_Gateway {

    private static Logger log = LoggerFactory.getLogger(S_Google_Gateway.class);

    @RequestMapping("/alexa/S_Google_Gateway/postGateway")
    public Object postGateway(HttpServletRequest request, HttpServletResponse response) throws Exception {

        com.alibaba.fastjson.JSONObject body = RequestParseParamsTool.getParamsFromRequestBody(request);
        log.info("body===>" + body);
        //设备发现
//        {"inputs":[{"intent":"action.devices.SYNC"}],"requestId":"7824531187848721189"}
        JSONArray inputs = body.getJSONArray("inputs");
        String requestId = body.getString("requestId");
        String intent = inputs.getJSONObject(0).getString("intent");
        SmartHomeResponse SyncResponse = new SmartHomeResponse();


        Map<String, Object> map = new HashMap<>();
        switch(intent) {
            case "action.devices.SYNC":
                SyncResponse.Payload payload = new SyncResponse.Payload();
                payload.setAgentUserId("1836.15267389");
                payload.setDevices(
                        new SyncResponse.Payload.Device[] {
                                new SyncResponse.Payload.Device.Builder()
                                        .setId("123")
                                        .setType("action.devices.types.OUTLET")
                                        .addTrait("action.devices.traits.OnOff")
                                        .setName(
                                                Collections.singletonList("My Outlet 1234"),
                                                "Night light",
                                                Collections.singletonList("Wall plug"))
                                        .setWillReportState(true)
                                        .setDeviceInfo("lights-out-inc", "hs1234", "3.2", "11.4")
                                        .setCustomData(
                                                new JSONObject()
                                                        .put("fooValue", 74)
                                                        .put("barValue", true)
                                                        .put("bazValue", "foo"))
                                        .build(),
                                new SyncResponse.Payload.Device.Builder()
                                        .setId("456")
                                        .setType("action.devices.types.LIGHT")
                                        .addTrait("action.devices.traits.OnOff")
                                        .addTrait("action.devices.traits.Brightness")
                                        .addTrait("action.devices.traits.ColorTemperature")
                                        .addTrait("action.devices.traits.ColorSpectrum")
                                        .setName(
                                                Collections.singletonList("Lights Out Inc. bulb A19 color hyperglow"),
                                                "Lamp",
                                                Collections.singletonList("Reading lamp"))
                                        .setWillReportState(true)
                                        .setDeviceInfo("Lights Out Inc.", "hg11", "1.2", "5.4")
                                        .setCustomData(
                                                new JSONObject()
                                                        .put("fooValue", 12)
                                                        .put("barValue", false)
                                                        .put("bazValue", "bar"))
                                        .build(),
                        });
                SyncResponse = new SyncResponse(requestId, payload);

                break;

            case "action.devices.QUERY":
                QueryResponse.Payload payload1 = new QueryResponse.Payload();
                payload1.setDevices(
                        new HashMap<String, Map<String, Object>>() {
                            {
                                put(
                                        "123",
                                        new HashMap<String, Object>() {
                                            {
                                                put("on", true);
                                                put("online", true);
                                            }
                                        });
                                put(
                                        "456",
                                        new HashMap<String, Object>() {
                                            {
                                                put("on", true);
                                                put("online", true);
                                                put("brightness", 80);
                                                put(
                                                        "color",
                                                        new HashMap<String, Object>() {
                                                            {
                                                                put("name", "cerulean");
                                                                put("spectrumRGB", 31655);
                                                            }
                                                        });
                                            }
                                        });
                            }
                        });

                SyncResponse = new QueryResponse(requestId, payload1);
                break;

            case "action.devices.EXECUTE":
                ExecuteResponse.Payload payload2 = new ExecuteResponse.Payload();

                payload2.setCommands(
                        new ExecuteResponse.Payload.Commands[] {
                                new ExecuteResponse.Payload.Commands(
                                        new String[] {"123"},
                                        "SUCCESS",
                                        new HashMap<String, Object>() {
                                            {
                                                put("on", true);
                                                put("online", true);
                                            }
                                        },
                                        null,
                                        null),
                                new ExecuteResponse.Payload.Commands(new String[] {"456"}, "ERROR", null, "deviceTurnedOff", null)
                        });
                SyncResponse = new ExecuteResponse(requestId, payload2);
                break;

            default:
                System.out.println("INVALID Namespace");
                break;

        }
        log.info("响应数据：" + SyncResponse.build().toString());
        return SyncResponse.build().toString();
    }

}